package com.si.controller.manage;

import com.si.entity.*;
import com.si.service.BlogrollService;
import com.si.service.FileDownloadService;
import com.si.service.OperationLogService;
import com.si.util.*;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 下载专区
 *
 * @author Dell
 */
@Controller
@RequestMapping("fileupload/manage")
public class FileUploudController {

    @Autowired
    private FileDownloadService fileDownloadService;
    @Autowired
    private OperationLogService operationLogService;
    /**
     * 下载专区主页
     *
     * @return
     */
    @RequestMapping(value = "list")
    public String load(ModelMap modelMap,
                       @RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
                       @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        QueryWhere.setOrderList(Order.desc("createDate"));
        Paging<FileDownload> paging = fileDownloadService.createCriteriaPaging(curPage, pageSize);
        modelMap.put("paging", paging);
        return "managePage/fileupload/list";
    }


    /**
     * 进入到添加页面
     *
     * @return
     */
    @RequestMapping(value = "initAdd")
    public String initAdd(ModelMap modelMap) {
        return "managePage/fileupload/updatesss";
    }


    /**
     * 保存数据
     *
     * @return
     */
    @RequestMapping(value = "save")
    public String save(HttpServletRequest request,
                       ModelMap modelMap,
                       @RequestParam(value = "name") String name,
                       @RequestParam("fileInput") MultipartFile file) throws IOException {

        //FileDownload fileDownload=(FileDownload)fileDownloadService.getObjectById(id);
        FileDownload fileDownload = new FileDownload();
        fileDownload.setCreateDate(new Date());

        if (!file.isEmpty()) {
            String filename = FileHandleUtil.Upset(file);
            fileDownload.setAddress(FileHandleUtil.upload(file.getInputStream(), "upload/fileDownload/", filename, request));
            fileDownload.setName(name);
            fileDownload.setSize(FileUpload.getPrintSize(file.getSize()));
            fileDownload.setCreateDate(new Date());
            fileDownloadService.save(fileDownload);
            OperationLog operationLog = new OperationLog();
            operationLog.setMiaoshu("执行成功！");
            operationLog.setOperationdetails("添加/修改下载专区\""+fileDownload.getName()+"\"");
            operationLog.setOperationtime(new Date().toString());
            operationLog.setSensitive("下载专区");
            String ip = IpAddressUtil.getIpAddress(request);
            operationLog.setIpadderse(ip);
            String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
            operationLog.setUsername(sysUser);
            operationLogService.save(operationLog);
        }


        return "redirect:/fileupload/manage/list.html";
    }

    /**
     * 根据id 删除
     *
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "delete/{id}")
    public String delete(
            @PathVariable(value = "id") Integer id,
            HttpServletRequest request,    RedirectAttributes redirectAttributes) {
        FileDownload fileDownload = (FileDownload) fileDownloadService.getObjectById(id);
        OperationLog operationLog = new OperationLog();
        operationLog.setMiaoshu("执行成功");
        operationLog.setOperationdetails("删除下载专区\""+fileDownload.getName()+"\"");
        operationLog.setOperationtime(new Date().toString());
        operationLog.setSensitive("下载专区");
        String ip = IpAddressUtil.getIpAddress(request);
        operationLog.setIpadderse(ip);
        String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
        operationLog.setUsername(sysUser);
        operationLogService.save(operationLog);
        fileDownloadService.delete(fileDownload);
        redirectAttributes.addFlashAttribute("message", "删除成功");
        return "redirect:/fileupload/manage/list.html";

    }
}
